﻿
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Claims;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Common;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Text;
using Models;
using Xhelper.ApiGate;
using Newtonsoft.Json;

namespace Web.Controllers
{

  /// <summary>
  /// AddFaceCheck 接口请求参数
  /// </summary>  
  public class AddFaceCheckInfo
  {
    /// <summary>
    /// 样品SysPrimaryKey列表
    /// </summary>
    public List<string> keylis { set; get; }

    /// <summary>
    /// 人脸图片
    /// </summary>
    public string imgBase64 { set; get; }
  }

  /// <summary>
  /// 试验监督
  /// </summary>
  [Authorize(Roles = "user")]
  [Route("api/[controller]")]
  public class TestSuperviseController : BaseController
  {
    /// <summary>
    /// 获取待试验样品列表
    /// </summary>
    /// <returns></returns>
    [HttpGet("[action]")]
    public async Task<IActionResult> GetSampleList()
    {
      var parms = new Dictionary<string, string> { ["testUnitId"] = testUnitId };

      if (!String.IsNullOrWhiteSpace(Request.Query["pageNumber"])) parms["pageNumber"] = Request.Query["pageNumber"];
      if (!String.IsNullOrWhiteSpace(Request.Query["pageSize"])) parms["pageSize"] = Request.Query["pageSize"];

      var res = TestSuperviseApi.GetSampleList(parms);

      return await PublicTool.ReturnSuccess(res);
    }

    /// <summary>
    /// 面部验证记录登记
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpPost("[action]")]
    public async Task<IActionResult> AddFaceCheck([FromBody] AddFaceCheckInfo model)
    {
      // 1、人脸识别
      if (!FaceCheckHelper.check(model.imgBase64,uid))
      {
        return await PublicTool.ReturnError("人脸识别未通过");
      }

      // 2、提交记录      
      string _keylis = "";
      List<Record> recordLis = new List<Record>();
      var _RecordDt = PublicTool.NowTS;
      foreach (var key in model.keylis)
      {
        var r = new Record
        {
          SysPrimaryKey = key,
          Uid = uid,
          Uname=uname,
          RecordDt = _RecordDt
        };
        recordLis.Add(r);
        _keylis += $",'{key}'";
      }
      // 2.1、本地保存提交记录 以备未来进行有效性验证
      FaceCheckLogLocal.InsertBulk(recordLis);

      // 2.2、构造需要登记的 SysPrimaryKey列表及验证相关信息
      _keylis = $"({_keylis.Substring(1)})";
      var parms = new Dictionary<string, string>
      {
        ["testUnitId"] = testUnitId,
        ["currentUserID"] = uid,
        ["currentUser"] = uname,
        ["recordDt"] = _RecordDt,
        ["keylis"] = _keylis
      };

      var res = TestSuperviseApi.AddFaceCheck(parms);
      return await PublicTool.ReturnSuccess(res);
    }

    [AllowAnonymous]
    [HttpGet("[action]")]
    public async Task<IActionResult> TT( ) {
      var res = PublicTool.GetReferenceImg("1234");      
      return await PublicTool.ReturnSuccess(res);
    }



  }
}
